/**
 * 
 */
package org.abrysov.android.lombarditens.activity;

import org.abrysov.android.lombarditems.MainActivity;
import org.abrysov.android.lombarditems.R;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.Toast;

/**
 * @author Alexander Brysov
 *
 */
public class NewItemPanel extends Activity implements OnClickListener{

	private final static String TAG = "_li_NewItemPanel.class";
	
	private FrameLayout mItemClickArea = null;
	private FrameLayout mClientClickArea = null;
	
	private Button mPutNewItem = null;
	private SQLiteDatabase db = null;
	
	private String mPositionInList = null;
	
	private EditText mNameOfItem = null;
	private EditText mCostOfItem = null;
	private EditText mAboutItem = null;
	private EditText mSerial = null;
	
	private RatingBar mRating = null;
	
	private RadioButton mNew = null;

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_item);
		
		db = openOrCreateDatabase(MainActivity.DATABASE_NAME, 
				MODE_PRIVATE, null);
		mPositionInList = "1";
		
		mRating = (RatingBar)findViewById(R.id.newitem_rb_rating);
		
		mNew = (RadioButton)findViewById(R.id.newitem_r_new);
	
		mItemClickArea = (FrameLayout)findViewById(R.id.newitem_fl_item_photo);
		mItemClickArea.setOnClickListener(this);
		
		mClientClickArea = (FrameLayout)findViewById(R.id.newitem_fl_client_photo);
		mClientClickArea.setOnClickListener(this);
		
		mPutNewItem = (Button)findViewById(R.id.newitem_b_put_new_item);
		mPutNewItem.setOnClickListener(this);
		
		mNameOfItem = (EditText)findViewById(R.id.newitem_et_name_item);
		mCostOfItem = (EditText)findViewById(R.id.newitem_et_cost);
		
		mAboutItem = (EditText)findViewById(R.id.newitem_et_about);
		mSerial  = (EditText)findViewById(R.id.newitem_et_serial);
		
		
	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.newitem_fl_item_photo:
			getPicture ();
			break;

		case R.id.newitem_fl_client_photo:
			getPicture ();
			break;
			
		case R.id.newitem_b_put_new_item:
			
			String sName = mNameOfItem.getText().toString();
			String sCoast = mCostOfItem.getText().toString();
			String sSerialNum = mSerial.getText().toString();
			Float fRating = mRating.getRating();
			int iUsed;
			if(mNew.isChecked()){
				iUsed = 0;
			}else{
				iUsed = 1;
			}
			
			String sAbout = mAboutItem.getText().toString();
			
			Log.d(TAG, "put to db : ["
					+ sName + "," + sCoast + "," + fRating.toString() + "]");
			
			if (sName != null && !sName.equals("")) {
				try {
					db.execSQL("INSERT INTO items VALUES ('" + mPositionInList +"' , '" + sName + "' , '" + sCoast + "' , '" +sSerialNum+ "', '" + fRating.toString() + "', '" + iUsed + "', '" + sAbout + "', 'path_client','path_item', 'no category')");
					Toast.makeText(getApplicationContext(), "DataBase was changed", Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					e.printStackTrace();
				}				
			}
			
			break;
			
		default:
			break;
		}
		
	}
	
	private void getPicture () {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		startActivityForResult(intent, 0);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();

		db.close();
	
	}
	
}
